package com.kaiwukj.android.ufamily.mvp.ui.page.mine.address;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerMineComponent;
import com.kaiwukj.android.ufamily.di.module.MineModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.MyAddressUpdateParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyAddressResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.MyAddressEditUrl)
public class MyAddressEditActivity extends BaseMvpActivity<MyAddressPresenter> implements MineContract.AddressView {

    private static final int PICK_COMMUNITY = 101;

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.rg_sex)
    RadioGroup rgSex;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_community)
    TextView tvCommunity;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_part_num)
    EditText etPartNum;//门牌号
    @BindView(R.id.switch_default)
    SwitchCompat switchDefault;

    @Autowired
    MyAddressResult addressResult;

    private CommunityResult communityResult;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick(R.id.container_community)
    void pickCommunity() {
        ARouter.getInstance().build(ARouterUrlKt.CommunityUpdateUrl)
                .withInt("pickAction", 1)
                .navigation(this, PICK_COMMUNITY);
    }

    @OnClick(R.id.btn_submit)
    void submit() {
        save();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerMineComponent.builder()
                .appComponent(appComponent)
                .mineModule(new MineModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_address_my_edit;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (addressResult != null) {
            initResultUI(addressResult);
        } else {
            initUI();
        }
    }

    private void initUI() {
        tvTitle.setText("添加地址");
        rgSex.check(R.id.rb_man);
        switchDefault.setChecked(true);
        communityResult = new CommunityResult();
        communityResult.setId(getApp().getUserInfo().getLastCommunityId());
        communityResult.setCommunityName(getApp().getUserInfo().getLastCommunityName());
        tvCommunity.setText(communityResult.getCommunityName());
    }


    private void initResultUI(MyAddressResult result) {
        communityResult = new CommunityResult();
        communityResult.setId(result.getSysCommunityId());
        communityResult.setCommunityName(result.getCommunityName());
        tvTitle.setText("修改地址");
        etName.setText(result.getConsigneeName());
        etPhone.setText(result.getPhone());
        rgSex.check(result.isMan() ? R.id.rb_man : R.id.rb_woman);
        etAddress.setText(result.getDetailAddress());
        switchDefault.setChecked(result.isDefault());
        tvCommunity.setText(result.getCommunityName());
        etPartNum.setText(result.getBuildingNumber());
    }

    @Override
    public void onFetchAddress(List<MyAddressResult> results) {

    }

    private void save() {
        String nickName = etName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        int sex = rgSex.getCheckedRadioButtonId() == R.id.rb_man ? 1 : 2;
        String buildNum = etPartNum.getText().toString().trim();
        String detailAddress = etAddress.getText().toString().trim();
        boolean isDefault = switchDefault.isChecked();

        MyAddressUpdateParams params = new MyAddressUpdateParams();
        if (StringUtils.isEmpty(nickName)) {
            showMessage("请输入收货人姓名");
            return;
        }
        if (StringUtils.isEmpty(phone)) {
            showMessage("请输入联系方式");
            return;
        }
        if (!RegexUtils.isMobileSimple(phone)) {
            showMessage("请输入正确的联系方式");
            return;
        }
        if (communityResult == null) {
            showMessage("请选择需要服务的小区");
            return;
        }
        if (StringUtils.isEmpty(detailAddress)) {
            showMessage("请输入详细地址");
            return;
        }
        if (StringUtils.isEmpty(buildNum)) {
            showMessage("请输入门牌号");
            return;
        }

        params.setConsigneeName(nickName);
        params.setPhone(phone);
        params.setGender(sex);
        params.setBuildingNumber(buildNum);
        params.setDetailAddress(detailAddress);
        params.setWhetherDefault(isDefault ? 1 : 0);
        params.setSysCommunityId(communityResult.getId());
        params.setCommunityName(communityResult.getCommunityName());
        if (addressResult != null) {
            params.setId(addressResult.getId());
            mPresenter.updateAddress(params);
        } else {
            mPresenter.addAddress(params);
        }
        showLoading("正在保存...");

    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        switch (taskId) {
            case MyAddressPresenter.ADD:
                showMessage("添加成功");
                break;
            case MyAddressPresenter.UPDATE:
                showMessage("更新成功");
                break;
        }
        hideLoading();
        setResult(RESULT_OK);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_COMMUNITY) {
            if (RESULT_OK == resultCode) {
                if (data != null && data.getExtras() != null) {
                    communityResult = (CommunityResult) data.getExtras().getSerializable("data");
                    if (communityResult != null) {
                        tvCommunity.setText(communityResult.getCommunityName());
                    }
                }
            }
        }
    }
}
